package com.hex.ds.hdrs.app.service.impl;

import com.hex.ds.hdrs.app.converter.IAppAgentRelationConverter;
import com.hex.ds.hdrs.app.dao.AppAgentRelationDao;
import com.hex.ds.hdrs.app.dto.AppAgentRelationDto;
import com.hex.ds.hdrs.app.po.AppAgentRelationPo;
import com.hex.ds.hdrs.app.service.IAppAgentRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Package com.hex.ds.hdrs.app.service.impl
 * @ClassName AppAgentRelationServiceImpl
 * @Description 应用与节点关联表业务实现
 * @Author yu liuyang
 * @Date 2023/4/16 10:25
 * @Version v1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppAgentRelationServiceImpl implements IAppAgentRelationService {

    private final AppAgentRelationDao appAgentRelationDao;

    private final IAppAgentRelationConverter appAgentConverter;

    /*
     * @Method: queryListByAppCode <br>
     * @Param: [appCode] <br>
     * @Return: java.util.List<com.hex.ds.hdrs.app.po.AppAgentRelationPo> <br>
     * @Description：根据应用编码查询对应的关联AGENT信息<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/19 19:55 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<AppAgentRelationPo> queryListByAppCode(String appCode) {
        return queryList(new AppAgentRelationPo().setAppCode(appCode));
    }

    /*
     * @Method: queryListByAgentCode <br>
     * @Param: [agentCode] <br>
     * @Return: java.util.List<com.hex.ds.hdrs.app.po.AppAgentRelationPo> <br>
     * @Description：根据AGENT编码查询对应的关联应用信息<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/19 19:55 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<AppAgentRelationPo> queryListByAgentCode(String agentCode) {
        return queryList(new AppAgentRelationPo().setAgentCode(agentCode));
    }

    /**
     * @Method queryList <br>
     * @Param [appAgentRelationDto] <br>
     * @Return java.util.List<com.hex.ds.hdrs.app.po.AppAgentRelation> <br>
     * @Description 查询应用与节点关联列表 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/17 15:46 <br>
     * @Version V1.0 <br>
     */
    private List<AppAgentRelationPo> queryList(AppAgentRelationPo appAgentRelationPo) {
        return appAgentRelationDao.queryList(appAgentRelationPo);
    }

    /**
     * @Method add <br>
     * @Param [appAgentRelationDto] <br>
     * @Return void <br>
     * @Description 添加应用与节点关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:26 <br>
     * @Version V1.0 <br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(List<AppAgentRelationDto> appAgentRelationDtoList, String appCode) {
        appAgentRelationDtoList.stream().forEach((appAgentRelationDto) -> {
            addRelation(appAgentRelationDto, appCode);
        });
    }

    /**
     * @Method addRelation <br>
     * @Param [appAgentRelation] <br>
     * @Return void <br>
     * @Description 添加应用与节点关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/18 16:11 <br>
     * @Version V1.0 <br>
     */
    private void addRelation(AppAgentRelationDto appAgentRelationDto, String appCode) {
        int count = appAgentRelationDao.insert(appAgentConverter.toAppAgentRelation(appAgentRelationDto, appCode));
        if (count != 1) {
            throw new RuntimeException("添加应用与节点关联失败！");
        }
    }

    /**
     * @Method modifyByAppCode <br>
     * @Param [appAgentRelationDto] <br>
     * @Return void <br>
     * @Description 根据应用编码修改应用与节点关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:28 <br>
     * @Version V1.0 <br>
     */
    public void modifyByAppCode(List<AppAgentRelationDto> appAgentRelationDtoList, String appCode) {
        deleteByAppCode(appCode);
        add(appAgentRelationDtoList, appCode);
    }

    /**
     * @Method deleteByAppCode <br>
     * @Param [appCode] <br>
     * @Return void <br>
     * @Description 根据应用编码删除应用与节点关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:27 <br>
     * @Version V1.0 <br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteByAppCode(String appCode) {
        appAgentRelationDao.deleteByAppCode(appCode);
    }

}
